package com.sinodata.bsm.cicp.service;

import java.util.*;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.cache.*;
import com.sinodata.bsm.cicp.task.TaskManager;
import com.sinodata.bsm.common.vo.*;

public class ResService {
    private static Logger logger = Logger.getLogger(ResService.class);
    private ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);
    private ResAttrValCache resAttrValCache = (ResAttrValCache) CacheFactory.getCache(ResAttrValCache.class);
    private ResRelationCache resRelationCache = (ResRelationCache) CacheFactory.getCache(ResRelationCache.class);

    private PropertyCache propertyCache = (PropertyCache) CacheFactory.getCache(PropertyCache.class);
    private AttributeCache attributeCache = (AttributeCache) CacheFactory.getCache(AttributeCache.class);

    public ResService() {

    }

    /**
     * 获取probe中的资源总数
     * 
     * @return
     */
    public String[] getResCount() {
        int normalRes = 0;
        int pausedRes = 0;
        List<Res> list = resCache.getALL();
        for (Res r : list) {
            if (r.getPaused() == 1) {
                pausedRes += 1;
            } else {
                normalRes += 1;
            }
        }
        return new String[] { list.size() + "", normalRes + "", pausedRes + "" };
    }

    public void addRes(Res res, ArrayList<ResAttrVal> attValList, ResRelation resRelation) {
        resCache.put(res.getId(), res);
        if (attValList != null) {
            for (ResAttrVal attVal : attValList) {
                resAttrValCache.put(attVal);
            }
        }
        if (resRelation != null && resRelation.getId() != null) {
            resRelationCache.addResRelation(resRelation);
        }
        TaskManager.getInstance().startResTask(res);
    }

    public void batchaddRes(ArrayList<Object[]> resList) {
        for (Object[] obj : resList) {
            Res res = (Res) obj[0];
            ArrayList<ResAttrVal> attValList = (ArrayList<ResAttrVal>) obj[1];
            ResRelation newResRelation = (ResRelation) obj[2];
            addRes(res, attValList, newResRelation);
        }
    }

    public void batchUpdateRes(ArrayList<Object[]> resList) {
        for (Object[] obj : resList) {
            Res res = (Res) obj[0];
            ArrayList<ResAttrVal> attValList = (ArrayList<ResAttrVal>) obj[1];
            ResRelation newResRelation = (ResRelation) obj[2];
            updateRes(res, attValList, newResRelation);
        }
    }

    public void updateRes(Res res, ArrayList<ResAttrVal> attValList, ResRelation newResRelation) {
        resCache.put(res.getId(), res);
        if (attValList != null) {
            for (ResAttrVal attVal : attValList) {
                resAttrValCache.put(attVal);
            }
        }
        if (newResRelation != null && newResRelation.getId() != null) {
            List<ResRelation> relList = resRelationCache.getResRelationsOfRes(res.getId());
            if (relList != null && newResRelation.getStartId() != null) {
                for (int j = relList.size() - 1; j >= 0; j--) {
                    ResRelation rel = relList.get(j);
                    if (rel.getStartId().longValue() == newResRelation.getStartId() && rel.getEndId().longValue() == res.getId()) {
                        resRelationCache.removeResRelation(rel);
                        break;
                    }
                }
            }
            resRelationCache.addResRelation(newResRelation);
        }
    }

    /**
     * 删除资源
     * 
     * @param resId
     */
    public void deleteRes(Long resId) {
        List<ResAttrVal> valList = resAttrValCache.getResAttrValByResId(resId);
        if (valList != null) {
            for (int i = valList.size() - 1; i >= 0; i--) {
                resAttrValCache.remove(resId, valList.get(i).getAttrId());
            }
        }
        resRelationCache.remove(resId);
        resCache.remove(resId);
        TaskManager.getInstance().deleteResTask(resId);
    }

    /**
     * 资源暂停
     * 
     * @param resId
     */
    public void setResPaused(Long resId) {
        Res res = resCache.get(resId.longValue());
        if (res == null)
            return;
        res.setPaused(1);
        resCache.put(resId.longValue(), res);
        TaskManager.getInstance().deleteResTask(resId);
    }

    /**
     * 资源监控恢复
     * 
     * @param resId
     */
    public void resumeResPaused(Long resId) {
        Res res = resCache.get(resId.longValue());
        if (res == null)
            return;
        res.setPaused(0);
        resCache.put(resId.longValue(), res);
        TaskManager.getInstance().restartResTask(resId.longValue());
    }

}
